package com.hongying.producer;


import com.hongying.config.DelayedQueueConfig;
import com.hongying.constant.RabbitMqConstant;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Component
@Log4j2
public class DelaySender {
    @Autowired
    private RabbitTemplate rabbitTemplate;


    public void sendDelayMsg(String message,Integer delayTime){
        log.info("当前时间:{},发送一条时长{}毫秒信息给延迟队列delayed.queue：{}",
                new Date().toString(),delayTime,message);
        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME,DelayedQueueConfig.DELAYED_ROUTING_KEY,message, msg -> {
            //设置发送的时候 延迟时长  单位：ms
            msg.getMessageProperties().setDelay(delayTime);
            return msg;
        });



    }


}
